var app = angular.module("stuManageApp", ["ngRoute"]);

app.config(function($routeProvider) {
	$routeProvider.when("/list", {
		controller: listController,
		templateUrl: "list.html"
	}).when("/view/:id", {
		controller: detailController,
		templateUrl: "detail.html"
	}).otherwise({
		redirectTo: "/list"
	})
})
//查询所有的信息
function listController($scope, $http,$rootScope) {
	$scope.sex="男";


//分页
	$scope.query=function(param){
		$http({
			method: 'GET',
			url: 'list',
//			headers: {
//				'Content-Type': 'application/x-www-form-urlencoded'
//			}, //设置headers
			params:param
		}).then(function(response) {
			$rootScope.list=response.data.data;//获取数组
			$scope.total=Math.ceil(response.data.total);//获取总页数
		}, function(response){
			alert("系统繁忙，请稍后再试");
		});
	}
	//查询所有学生信息
	$scope.query($scope.page);
		$scope.page=1;
	$scope.replace=function(page){
		if(page<1){
			page=1;return;
		}
		if(page>$scope.total){
			page=$scope.total;
			return;
		}
		$scope.query(page);
		$scope.page=page;
	}
	
	//全选
	$scope.checks=false;
	$scope.checkAll=function(){
		$scope.checks=!$scope.checks;
	}
	//全选删除
	$scope.delAll=function(){
		var boxList=angular.element("input[type=checkbox]:checked");
		if(confirm("确定要删除以上"+boxList.length+"条信息吗？"))
		{
			boxList.each(function(index,ele){
				console.info(ele.value);
				$scope.deleteUser(ele.value);
			})
		}
	}
	

	//删除一个学生
	$scope.del = function(index) {
		if(!confirm("确定要删除吗")) return;
		$scope.deleteUser(index);
	}
	$scope.deleteUser=function(index){
		$http({
			method: 'POST',
			url: 'del',
			headers: {
				'Content-Type': 'application/x-www-form-urlencoded'
			}, //设置headers
			data: "id=" + index
		}).then(function(response) {
			if(response.data == "success") {
				location.reload();
			} else {
				alert("系统繁忙，请稍后再试");
			}
		}, function(response) {
			alert("系统繁忙，请稍后再试");
		});
	}

	
	
	

	//添加一个学生
	$scope.save=function(){
		var obj={
			name:$scope.name,
			age:$scope.age,
			sex:$scope.sex
		}
		if($scope.id || $scope.id==0){
			obj.id=$scope.id;
		}
		$http({
			method: 'POST',
			url: 'save',
			headers: {
				'Content-Type': 'application/x-www-form-urlencoded'
			}, //设置headers
			data: $.param(obj)
		}).then(function(response) {
			if(response.data == "success") {
				location.reload();
			} else {
				alert("系统繁忙，请稍后再试");
			}
		}, function(response) {
			alert("系统繁忙，请稍后再试");
		});
	}

	//修改数据
	$scope.update=function(id,index){
		var user=$rootScope.list[index];
		$scope.name=user.username;
		$scope.age=parseInt(user.userage);
		$scope.sex=user.usersex;
		$scope.id=id;
	}	
	$scope.orderby=true;
	$scope.order=1;
	$scope.sortQuery=function(){
		$scope.orderby=!$scope.orderby;
		$scope.order=$scope.orderby?1:-1;
		$scope.query({page:$scope.page,age:$scope.order});
	}
	
	
	
	//点击搜索事件
	$scope.search=function(){
		var a=$scope.txt;
			$http({
				method:'POST',
				url:'search',
				headers:{'Content-Type': 'application/x-www-form-urlencoded'},
				data:'txt='+$scope.txt
			}).then(function(response){
				console.log(response)
				$rootScope.list=response.data
			},function(response){
				alert('系统繁忙，请稍后再试');
			});
		}

}

function detailController($scope,$routeParams,$rootScope) {
	var id = $routeParams["id"];
	$scope.user=$rootScope.list[id];
}

